<template>
	<view class="order">
		<uni-nav-bar left-icon="arrowleft" right-icon="search" title="我的订单" @clickLeft="back" @clickRight="find" :fixed="true" status-bar class="top"></uni-nav-bar>
		<view class="statistics">
			<view class="ul">
				<view class="li">
					订单总数：
					<text>{{whole.totalCnt}}</text>

				</view>
				<view class="li">
					订单总额：
					<text>{{whole. totalSum}}</text>
				</view>
			</view>
			<view class="ul" v-if="this.active==0||this.active==2">
				<view class="li">
					需收款合计：
					<text> {{whole.receivable}}元</text>
				</view>
				<view class="li">
					上交款合计：
					<text> {{whole.upPayment}}元</text>
				</view>
			</view>
		</view>
		<view class="uni-flex">
		    <view class="flex-item" :class="{ active: active == 0 }" @click="cut(0)"><text>全部</text></view> 
			<view class="flex-item" :class="{ active: active == 1 }" @click="cut(1)"><text>已提交</text></view>
			<view class="flex-item" :class="{ active: active == 2 }" @click="cut(2)"><text>待审核</text></view>
			<view class="flex-item" :class="{ active: active == 4 }" @click="cut(4)"><text>已审核</text></view>
			<view class="flex-item" :class="{ active: active == 3 }" @click="cut(3)"><text>已认领</text></view>
		</view>
		<view class="list">
			<view class="li" v-for="(item,index) in dataList" :key="index">
				<view class="shop">
					<image :src="item.image" class="imgthumb" :lazy-load="true" mode="aspectFill" />
					<view class="msg" @click="go_details(item.orderId)">
						<view class="order black">订单号：{{item.orderSn}}</view>
						<view class="time gray ">下单时间：{{item.createdTime}}</view>
						<view v-for="(item1,index1) in item.sku" :key="index1">
							<view class="sizr gray " v-if="index1==0">颜色：{{item1.split(":")[1]}}</view>
							<view class="sizr gray " v-if="index1==1">尺码：{{item1.split(":")[1]}}</view>
						</view>
						<view class="time gray ">订单来源：<text v-if="item.orderFrom.value==3">线上</text><text v-else>线下</text></view>
					</view>
					
				</view>
				<view class="genre">
					<view class="msg">
						共1件
						<view class="price">
							合计：
							<text>¥{{item.orderPrice}}</text>
						</view>
					</view>
					<view class="audit" v-if="active==2" @click="go_examine(item.orderId)">审核</view>
					<view class="xiugai"  v-if="item.canDel==1" @click="getshopDate(item.goodsId,item.orderId)">修改</view>
					<view class="xiugai deletes"  v-if="item.canDel==1" @click="deletes(item.orderId)">删除</view>
				</view>
			</view>
		</view>
		<view v-if="dataList.length==0" class="notavailable">
			<image src="../../../static/img/notavailable.png" mode="widthFix"></image>
			<view class="nottext">暂时没有数据</view>
		</view>
		<view class="pagination" v-if="pagination">
			<view class="left" :class="{ forbid: paging.page == 1 }" @click="prev">
				<uni-icons type="arrowleft" size="20" color="#fff" class="icons" />
				上一页
			</view>
			<view class="center">{{ paging.page }}/{{ paging.total }}</view>
			<view class="right" :class="{ forbid: paging.page == paging.total }" @click="next">
				下一页
				<uni-icons type="arrowright" size="20" color="#fff" class="icons" />
			</view>
		</view>
		<transition name="fade">
			<view class="ceng" v-show="is_preposition" @click="ceng"></view>
		</transition>
		<view class="preposition" v-show="is_preposition">
			<view class="intro" >
				<view class="img">
					<image :src="productImg" mode="aspectFill" />
				</view>
				<view class="text">
					<view class="name">{{goodDetail.goodsName }}</view>
					<view class="price">￥{{ disPrice }}</view>					
				</view>
				<view class="close" @click="ceng">
					×
				</view>
			</view>
			<view class="parameter">
				<view class="name">
					选择颜色/尺码
					<i class="cubeic-arrow right"></i>
				</view>
			
				<view class="ul" v-if="shop_details[0]">
					<view class="flex">
						<view class="li" v-for="(item, index1) in shop_details[0].sublevel" :key="index1" :class="{ active:index1 == indexs}"
						 @click="checkeds(item, index1)">
							{{ item }}
						</view>
					</view>
					<view class="flex">
						<view class="li" v-for="(item, index) in shop_details[0].size" :key="index" :class="{ active: index == idx}"
						 @click="checkcolor(item, index)">
							{{ item }}
						</view>
					</view>
			
				</view>
			
			</view>
			<view class="button" @click="sub()">确 定</view>
		</view>
	</view>
</template>

<script>
	import {
		uniNavBar,
		uniIcons
	} from '@dcloudio/uni-ui';
	import order from '../../../servers/agent/order/order.js';
	import goodDetails from '../../../servers/commodity/particulars.js';
	export default {
		components: {
			uniNavBar,
			uniIcons
		},
		data() {
			return {
				active: 0,
				dataList: [],
				whole: '',
				myList: [],
				paging: {
					page: 1,
					total: 100
				},
				pagination:true,
				is_preposition: 0,
				goodDetail: [],
				shop_details: [],
				productImg: '',
				disPrice:'',
				checked:'',
				checked1:'',
				idx: 0,
				indexs: 0,
				orderId:''
			};
		},
		mounted() {
			this.initData();
		},
		methods: {

			initData() { /* 默认初始化提交订单页面 */
				console.log(2342423423);
				uni.showLoading({
					title: '数据加载中'
				});
				var flag = this.active;
				// 订单统计
				var type=this.active+1;
				if(this.active==4){
					type=4
				}
				if(this.active==3){
					type=7
				}
				order.statistics(type)
					.then(res => {
						// console.log(res.data)
						this.whole = res.data;
				
						uni.hideLoading();
					})
					.catch(err => {
						console.log(new Error('请检查网络后重试'));
					});
				if(0==flag){
					this.dataList = [];
					var obj={
						currentPage: this.paging.page,
						pageSize: 10
					}
					order.allList(obj)
						.then(res => {
							console.log(res)
							if(res.code==200){
								if(res.data.records.length>0){
									this.paging.total = Math.ceil(res.data.total / res.data.size);
									this.dataList = res.data.records.map(v => {
										v.image = this.imgCDNUrl + v.productImg;
										var sku = [];
										if (v.productSkuDesp) {
											sku = v.productSkuDesp.split(",");
										}
										v.sku = sku;
										return v;
									});
									this.pagination=true
								}else{
									this.pagination=false
								}
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
							// this.whole=res.data.whole
							console.log(this.dataList)
							uni.hideLoading();
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
						});
				}
				if (1 == flag) {
					this.dataList = [];
					var obj={
						currentPage: this.paging.page,
						pageSize: 10
					}
					order.privatelist(obj)
						.then(res => {
							if(res.code==200){
								if(res.data.records.length>0){
									this.paging.total = Math.ceil(res.data.total / res.data.size);
									this.dataList = res.data.records.map(v => {
										v.image = this.imgCDNUrl + v.productImg;
										var sku = [];
										if (v.productSkuDesp) {
											sku = v.productSkuDesp.split(",");
										}
										v.sku = sku;
										return v;
									});
									this.pagination=true
								}else{
									this.pagination=false
								}
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
							// this.whole=res.data.whole
							console.log(this.dataList)
							uni.hideLoading();
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
						});
				}
				//待审核
				if (2 == flag) {
					this.dataList = [];
					var obj={
						currentPage: this.paging.page,
						pageSize: 10
					}
					console.log("flag:"+flag)
					order.toAudit(obj)
						.then(res => {
							if(res.code==200){
								if(res.data.records.length>0){
									this.paging.total = Math.ceil(res.data.total / res.data.size);
									this.dataList = res.data.records.map(v => {
										v.image = this.imgCDNUrl + v.productImg;
										var sku = [];
										if (v.productSkuDesp) {
											sku = v.productSkuDesp.split(",");
										}
										v.sku = sku;
										return v;
									});
									this.pagination=true
								}else{
									this.pagination=false
								}
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
							
							// this.whole=res.data.whole
							uni.hideLoading();
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
						});
				}
				//已审核
				if (4 == flag) {
					this.dataList = [];
					var obj={
						currentPage: this.paging.page,
						pageSize: 10
					}
					
					order.hasAudit(obj)
						.then(res => {
							if(res.code==200){
								if(res.data.records.length>0){
									this.paging.total = Math.ceil(res.data.total / res.data.size);
									this.dataList = res.data.records.map(v => {
										v.image = this.imgCDNUrl + v.productImg;
										var sku = [];
										if (v.productSkuDesp) {
											sku = v.productSkuDesp.split(",");
										}
										v.sku = sku;
										return v;
									});
									this.pagination=true
								}else{
									this.pagination=false
								}
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
							
							// this.whole=res.data.whole
							uni.hideLoading();
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
						});
				}
				if (3 == flag) {
					this.dataList = [];
					var obj={
						currentPage: this.paging.page,
						pageSize: 10
					}
					order.cliamList(obj)
						.then(res => {
							if(res.code==200){
								if(res.data.records.length>0){
									this.paging.total = Math.ceil(res.data.total / res.data.size);
									this.dataList = res.data.records.map(v => {
										v.image = this.imgCDNUrl + v.productImg;
										var sku = [];
										if (v.productSkuDesp) {
											sku = v.productSkuDesp.split(",");
										}
										v.sku = sku;
										return v;
									});
									this.pagination=true
								}else{
									this.pagination=false
								}
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
							// this.whole=res.data.whole

							uni.hideLoading();
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
						});
				}
				

			},
			back() {
				uni.navigateTo({
					url: '/pages/agent/index'
				});
			},
			deletes(id){
				var _this=this;
				uni.showModal({
				    title: '提示',
				    content: '确定要删除该订单吗？',
				    success: function (res) {
				        if (res.confirm) {
				            order.del(id)
				            	.then(res => {			
				            		if(res.code==200){
				            			uni.showToast({
				            				title:'删除成功'							
				            			})
				            			_this.initData();
				            		}else{
				            			uni.showToast({
				            				title:res.msg,
				            				icon:'none'
				            			})
				            		}
				            		
				            		
				            		
				            	})
				            	.catch(err => {
				            		console.log(new Error('请检查网络后重试'));
				            	});
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
				
			},
			ceng() {
				this.is_preposition = false;
				this.is_share = false;
			},
			// 选择尺寸
			checkeds(e, ind) {
				this.indexs = ind
				this.checked = e
				this.goodDetail.skus.map(v=>{
					if(v.productSkuDesp.split(',')[0].split(':')[1] ==this.checked ){
						this.productImg = this.imgCDNUrl + v.productImg
						this.disPrice=v.marketPrice
					}
				})
			},
			// 选择颜色
			checkcolor(e, ind) {
				this.idx = ind
				this.checked1 = e
				this.goodDetails.skus.map(v=>{
					if(v.productSkuDesp.split(',')[1].split(':')[1] ==this.checked1 ){
						
						this.disPrice=v.marketPrice
					}
				})
			},
			getshopDate(e,id) {
					this.is_preposition=true
					this.orderId=id
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					goodDetails
						.goodDetails(e)
						.then(res => {
							 this.goodDetail = res.data;
							console.log(res)
							this.goodImag = this.goodDetail.imgs.map(v => {
								v = this.imgCDNUrl + v;
								return v;
							});
							console.log(this.goodImag[0])
							if (res.data.skus.length>0) {
								this.productImg = this.imgCDNUrl + this.goodDetail.skus[0].productImg;
							} else {
								
								this.productImg = this.goodImag[0];
							}
							this.disPrice=this.goodDetail.skus[0].marketPrice
							console.log(res.data,this.goodDetail.skus.length)
							uni.hideLoading();
							this.screen();
						})
						.catch(err => {
							console.log(err)
							console.log(new Error('请检查网络后重试'));
						});
				
				
			},
			screen() {
				var classify = [];
				var it1 = [],it2 = [];
				var da1 = this.goodDetail.skus;
				var da10 = [];
				for (var i = 0; i < da1.length; i++) {
					var da2 = da1[i].productSkuDesp;
					var da3 = da2.split(',');
			
					it1.push(da3[0].split(':')[1])
					it2.push(da3[1].split(':')[1])
			
					for (var j = 0; j < it1.length; j++) {
						if (da10.indexOf(it1[j]) === -1) {
							da10.push(it1[j])
						}
					}
					for (var k = 0; k < it2.length; k++) {
						if (classify.indexOf(it2[k]) === -1) {
							classify.push(it2[k])
						}
					}		
				}
				this.shop_details.push({
					size: classify,
					sublevel: da10
				});
				console.log(this.shop_details)
				this.checked1 = this.shop_details[0].size[0]
				this.checked = this.shop_details[0].sublevel[0]
				this.indexs = 0
				this.idx = 0
			},
			
			sub() {
					uni.showLoading({
						title: '提交中',
						mask: true
					});
					var productId = '',
						skus = this.goodDetail.skus;
				
					for (var i = 0; i < skus.length; i++) {
						var size = skus[i].productSkuDesp.split(',')[1].split(':')[1]
						var color = skus[i].productSkuDesp.split(',')[0].split(':')[1]
						if (this.checked == color && this.checked1 == size) {
							
							productId = skus[i].id;
						}
					}
			
					var da = {
						itemId: productId, // 货品id
						orderId : this.orderId
					};
					
					order.updateOrderGoods(da)
						.then(res => {
						
							this.is_preposition=false
							uni.hideLoading();
							this.screen();
							this.initData();
						})
						.catch(err => {
							console.log(err)
							console.log(new Error('请检查网络后重试'));
						});
				
			},
			cut(e) {
				this.active = e;
				this.paging.page= 1;
				this.initData(); //初始化数据
			},
			find() {
				uni.navigateTo({
					url: './orderfind'
				});
			},
			go_examine(orderId) {

				uni.navigateTo({
					url: '/pages/agent/order/orderclaim?id=' + orderId
				});

			},
			go_details(orderId) {

				if (this.active != 2) { /* 审核订单不跳转 */
					uni.navigateTo({
						url: '/pages/agent/order/orderdetails?id=' + orderId
					});
				}


			},
			next() {
				if (this.paging.page < this.paging.total) {
					this.paging.page += 1;
					this.initData();
				}
			},
			prev() {
				if (this.paging.page > 1) {
					this.paging.page -= 1;
					this.initData();
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	page {
		background: #ffffff;
	}
	
.preposition {
			position: fixed;
			bottom: 0;	
			width: 100%;
			background: #fff;
			z-index: 100;

			.intro {
				position: relative;
				display: flex;
				padding: 49rpx 20rpx;

				.img {
					margin-right: 30rpx;

					image {
						width: 211rpx;
						height: 211rpx;
					}
				}

				.text {
					padding-right: 65rpx;

					.name {
						font-size: 28rpx;
						line-height: 40rpx;
					}

					.price {
						position: absolute;
						bottom: 49rpx;
						color: #bd3c8c;
						font-size: 36rpx;
					}
					.original {
						font-size: 30rpx;
						text-decoration: line-through;
						color: #999;
					}
					
				}

				.close {
					position: absolute;
					top: 40rpx;
					right: 20rpx;
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					border: 1px solid #9b9b9b;
					/*no*/
					text-align: center;
					color: #9b9b9b;
					line-height: 40rpx;

					.uni-icons {
						position: absolute;
						left: 0;
						top: 0;
						font-size: 40rpx;
						
					}
				}
			}

			.parameter {
				padding: 0 20rpx;

				.ul {
					margin-bottom: 10rpx;
					margin-top: 20rpx;
					.p {
						font-size: 28rpx;
						line-height: 57rpx;
					}

					.li {
						display: inline-block;
						border-radius: 6rpx;
						padding: 19rpx 24rpx;
						border: 1px solid #efefef;
						/*no*/
						margin-right: 20rpx;
						margin-bottom: 20rpx;
						font-size: 30rpx;
					}

					.li.active {
						background: #bd3c8c;
						color: #fff;
						border-color: #bd3c8c;
					}
				}

				.input {
					.inp {
						width: 100%;
						border: 1px solid #efefef;
						line-height: 80rpx;
						height: 80rpx;
						padding: 20rpx;
						margin: 20rpx 0;
						box-sizing: border-box;
						border-radius: 6rpx;
						font-size: 28rpx;
					}
				}
			}

			.button {
				margin: 80rpx 20rpx 20rpx;
				background: linear-gradient(80deg, #d2158a, #fc6395);
				text-align: center;
				line-height: 90rpx;
				font-size: 32rpx;
				color: #fff;
				border-radius: 45rpx;
			}
		}
	.order {
		.uni-flex {
			// position: fixed;
			z-index: 1;
			margin-top: 40rpx;
			// top: 44px;
			width: 100%;
			display: flex;
			flex-direction: row;
			box-shadow: 5rpx 0px 20rpx #c5c5c5;

			.flex-item {
				font-size: 28rpx;
				font-weight: 900;
				width: 33.33%;
				height: 89rpx;
				text-align: center;
				line-height: 89rpx;
				background: #ffffff;
			}

			.flex-item.active {
				text {
					color: #bd3c8c;
					border-bottom: 3rpx solid #bd3c8c;
				}
			}
		}

		.statistics {
			margin: 20px 20rpx 0;
			background: #ffffff;
			border-radius: 10rpx;
			box-shadow: 5rpx 0px 20rpx #c5c5c5;

			.ul {
				padding: 10rpx;
				line-height: 117rpx;
				display: flex;
				flex-direction: row;
				font-size: 26rpx;
				border-bottom: 1px solid #e1e1e1;

				.li {
					width: 50%;

					text {
						font-size: 32rpx;
						color: #f70225;
					}
				}

				.li:nth-child(1) {
					padding-left: 20rpx;
				}
			}
		}

		.list {
			border-top: 26rpx solid #f6f6f6;
			padding: 0 20rpx;

			.li {
				padding: 50rpx 0;
				border-bottom: 1px solid #e1e1e1;

				.shop {
					position: relative;
					display: flex;
					flex-direction: row;

					image {
						width: 230rpx;
						height: 230rpx;
						border-radius: 10rpx;
					}

					.msg {
						width: 431rpx;
						margin-left: 37rpx;
						line-height: 46rpx;

						.order {
							font-size: 28rpx;
						}

						.type {
							font-size: 26rpx;
						}

						.gray {
							color: #999999;
							font-size: 26rpx;
							line-height: 42rpx;
						}
					}

					
				}

				.genre {
					color: #bd3c8c;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					padding-top: 57rpx;
					
					image {
						position: relative;
						width: 42rpx;
						height: 41rpx;
						top: 5rpx;
						margin-right: 15rpx;
					}
					.audit {
						
						width: 120rpx;
						height: 60rpx;
						border-radius: 60rpx;
						border: 1px solid #f70225;
						color: #f70225;
						text-align: center;
					}
					.xiugai{
						width: 120rpx;
						height: 60rpx;
						border-radius: 60rpx;
						background-color:  #f70225;
						color: #fff;
						text-align: center;
					}
					.deletes{
						background-color:  #fff;
						border:1px solid #f70225;
						color: #f70225;
					}
					.orderfrom {
						width: 322rpx;
					}

					.msg {
						color: #999999;
						font-size: 26rpx;

						.price {
							display: inline-block;
							color: #333333;
							margin-left: 29rpx;

							text {
								font-size: 30rpx;
								color: #333333;
							}
						}
					}
				}
			}

			.li:nth-last-child(1) {
				border-bottom: none;
			}
		}

		.pagination {
			margin: 50rpx 20rpx;
			text-align: center;
			line-height: 80rpx;

			.center {
				width: 176rpx;
				height: 80rpx;
				display: inline-block;
				margin: 0 43rpx;
				color: #666666;
				background: #ffffff;
				border-radius: 6rpx;
				border: 1px solid #d0d0d0;
				box-sizing: border-box;
			}

			.left,
			.right {
				width: 220rpx;
				height: 80rpx;
				background: #bd3c8c;
				color: #fff;
				border-radius: 6rpx;
				box-sizing: border-box;
			}

			.forbid {
				background: #ffffff;
				border: 1px solid #d0d0d0;
				color: #aaaaaa;

				.icons {
					color: #aaaaaa !important;
				}
			}
		}
	}
</style>
